package com.beerandjesus.photogallery.page;

public class ZeroIndexListPagingStrategy implements ListPagingStrategy {

    public int startIndex(int page, int numPerPage, int total) {
        if (page < 1 || numPerPage < 1 || total < 1) {
            return -1;
        }
        if (page == 1) {
            return 0;
        }
        int index = (page - 1) * numPerPage;
        if (index >= total) {
            return -1;
        }
        if (total <= numPerPage) {
            return numPerPage - 1;
        }

        return index;
    }

    public int endIndex(int page, int numPerPage, int total) {
        int startIndex = startIndex(page, numPerPage, total);
        if (startIndex == -1) {
            return -1;
        }
        int lastIndex = total - 1;
        if (startIndex + numPerPage <= lastIndex) {
            return startIndex + numPerPage - 1;
        }
        return lastIndex;
    }

    public int lastPage(int numPerPage, int total) {
        if (numPerPage < 1 || total < 1) {
            return -1;
        }
        if (total % numPerPage == 0) {
            return total / numPerPage;
        } else {
            return total / numPerPage + 1;
        }
    }
}
